using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using HCD.Const;
using HCD.Entities;
using HCD.Mdt.Dtos;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace HCD.Mdt
{
    [Route("api/[controller]/[action]"),ApiController]
    public class MdtMasterAppService : AsyncCrudAppService<MdtMaster, MdtMasterDto, long, MdtMasterGetAllInput, MdtMasterAddInput, MdtMasterEditInput, EntityDto<long>, EntityDto<long>>, IMdtMasterAppService
    {
        private IRepository<MdtMaster, long> MdtMasterRep { get; set; }
        public IRepository<MdtGroup, long> MdtGroupRep { get; set; }
        public IRepository<MdtGroupUser, long> MdtGroupUserRep { get; set; }
        public MdtMasterAppService(IRepository<MdtMaster, long> repository)
        : base(repository)
        {
            MdtMasterRep = repository;
            LocalizationSourceName = EnterpriseConsts.LocalizationSourceName;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, ActionName("Add"), NonAction]
        public override async Task<MdtMasterDto> CreateAsync(MdtMasterAddInput input)
        {
            return await base.CreateAsync(input);
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut, ActionName("Edit"), NonAction]
        public override async Task<MdtMasterDto> UpdateAsync(MdtMasterEditInput input)
        {
            return await base.UpdateAsync(input);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete, ActionName("Delete"), NonAction]
        public override  async Task DeleteAsync(EntityDto<long> input)
        {
            await base.DeleteAsync(input);
        }
        /// <summary>
        /// 获取单个Mdt会诊记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet, ActionName("Get")]
        public override async Task<MdtMasterDto> GetAsync([FromQuery]EntityDto<long> input)
        {
            return await base.GetAsync(input);
        }
        /// <summary>
        /// 获取Mdt会诊分页记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet, ActionName("GetList")]
        public override async Task<PagedResultDto<MdtMasterDto>> GetAllAsync([FromQuery]MdtMasterGetAllInput input)
        {
            return await base.GetAllAsync(input);
        }

        protected override IQueryable<MdtMaster> CreateFilteredQuery(MdtMasterGetAllInput input)
        {
            return base.CreateFilteredQuery(input)
                .WhereIf(input.Status.HasValue, t => t.Status == input.Status.Value);
        }
        /// <summary>
        /// Mdt会诊申请
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task MdtMasterApply(MdtMasterApplyInput input)
        {
            var mdtMaster = ObjectMapper.Map<MdtMaster>(input);
            var mdtGroup = ObjectMapper.Map<MdtGroup>(input);
            mdtMaster.GroupId = await MdtGroupRep.InsertAndGetIdAsync(mdtGroup);
            mdtMaster.Id = await MdtMasterRep.InsertAndGetIdAsync(mdtMaster);
            foreach (var item in input.MdtGroupUserAdds)
            {
                var mdtGroupUser = ObjectMapper.Map<MdtGroupUser>(item);
                mdtGroupUser.GroupId = mdtMaster.GroupId;
                await MdtGroupUserRep.InsertAsync(mdtGroupUser);
            }
        }
    }
}


